import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class DiceUI extends JFrame {

	private JPanel contentPane;
	private ImageIcon diceOne;
	private ImageIcon diceTwo;
	private ImageIcon diceThree;
	private ImageIcon diceFour;
	private ImageIcon diceFive;
	private ImageIcon diceSix;
	private JLabel currentDiceOne;
	private JLabel currentDiceTwo;
	private JButton rollDiceButton;
	private Timer gear;
	private Dice dice;
	private JLabel label;
	private Player p;
	
	public DiceUI(Player play) {
		this.p=play;
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		this.setBounds(102, 103, 450, 300);
		this.contentPane = new JPanel();
		this.contentPane.setLayout(null);
		getContentPane().add(this.contentPane);
		this.setVisible(true);
		this.setSize(400,335);
	    this.setResizable(false);
	    
	    this.diceOne = new ImageIcon("Graphics/Dice-1.jpg");
	    this.diceTwo = new ImageIcon("Graphics/Dice-2.jpg");
	    this.diceThree = new ImageIcon("Graphics/Dice-3.jpg");
	    this.diceFour = new ImageIcon("Graphics/Dice-4.jpg");
	    this.diceFive = new ImageIcon("Graphics/Dice-5.jpg");
	    this.diceSix = new ImageIcon("Graphics/Dice-6.jpg");
	    
	    this.currentDiceOne = new JLabel(this.diceOne);
	    this.currentDiceTwo = new JLabel(this.diceOne);
	    this.rollDiceButton = new JButton("ROLL BABY!");
	    this.dice = new Dice();
	    
	    this.contentPane.add(this.currentDiceOne);
	    this.contentPane.add(this.currentDiceTwo);
	    this.contentPane.add(this.rollDiceButton);
	    
	    this.currentDiceOne.setBounds(10, 20, 176, 208);
	    this.currentDiceTwo.setBounds(200, 20, 176, 208);
	    this.rollDiceButton.setBounds(131, 260, 135, 23);
	    this.rollDiceButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
	    
	    label = new JLabel("New label");
	    label.setBounds(20, 239, 12, 12);
	    contentPane.add(label);
	    
	    this.currentDiceOne.setVisible(true);
	    this.currentDiceTwo.setVisible(true);
	    this.rollDiceButton.setVisible(true);
	    
	    
	    RollEvent rollEvent = new RollEvent();
        rollDiceButton.addActionListener(rollEvent);
		
	}
	
	private class RollEvent implements ActionListener {
		int limit = 3;
		
		public void actionPerformed(ActionEvent e){
			GearClass gc = new GearClass(limit);
			gear = new Timer(500, gc);
			Sound sound = new Sound();//sounds
			sound.play("Sounds/drumRoll.wav");
			gear.start();
			rollDiceButton.setEnabled(false);
			
			}
		
	}
	
	private class GearClass implements ActionListener {
		int limit;
		int count=0;
		
		public GearClass(int limit){
			this.limit = limit;
		}
		
		public void actionPerformed(ActionEvent gc){

			if(this.count<=this.limit){
				this.count++;
				rollDice();
				//Toolkit.getDefaultToolkit().beep();
			}
			else{
				if(p.getCurrentBlockPos().getBlockNumber() != 40)//bry
					movePlayer();
				else
					jailDiceSequence();
					gear.stop();
					dispose();
				
			}
				
			
			
		}
	}
	
	public void rollDice(){
	
		this.dice.rollDice1();
		this.dice.rollDice2();
		this.dice.setTotalRoll();
		this.setDiceRolled(this.dice.getRollDice1(), this.dice.getRollDice2());
		
	}
	
	public void jailDiceSequence(){//bry

		if(this.p.getJailCounter()!=2){
			
			if(this.dice.getRollDice1() == this.dice.getRollDice2()){
	
				SendOutOfJailCommand sendOutOfJailCommand = new SendOutOfJailCommand();
				sendOutOfJailCommand.execute();
				NextPlayerCommand nextPlayerCommand = new NextPlayerCommand();
				nextPlayerCommand.execute();
	
			}else{
	
				p.setJailCounter(p.getJailCounter()+1);
				NextPlayerCommand nextPlayerCommand = new NextPlayerCommand();
				nextPlayerCommand.execute();
	
			}
		}else{
			SendOutOfJailCommand sendOutOfJailCommand = new SendOutOfJailCommand();
			sendOutOfJailCommand.execute();
			p.setJailCounter(0);
			movePlayer();
		}
	}
	
	public void movePlayer(){
		//this.boardUI.moveEvent(this.dice.getTotalRoll());
		/*Fixing this party right now*/
		this.p.setLastRoll(this.dice.getTotalRoll());
		if(this.dice.getRollDice1()==this.dice.getRollDice2()){
			this.p.setDoubleRollCount(this.p.getDoubleRollCount()+1);
		}else{
			this.p.setDoubleRollCount(0);
		}
		
		Command cmd = new MovePlayerCommand();
		cmd.execute();
	}
	
	public void isDoubleRoll(int doubleRoll){
		
	}
	
	public void setDiceRolled(int x, int y){
		
		this.currentDiceOne.setVisible(false);
		this.currentDiceTwo.setVisible(false);
		
		switch(x){
		
			case 1: this.currentDiceOne=new JLabel(this.diceOne);
					this.contentPane.add(this.currentDiceOne);
					this.currentDiceOne.setBounds(10, 20, 176, 208);
					this.currentDiceOne.setVisible(true);
					break;
					
			case 2: this.currentDiceOne=new JLabel(this.diceTwo);
					this.contentPane.add(this.currentDiceOne);
					this.currentDiceOne.setBounds(10, 20, 176, 208);
					this.currentDiceOne.setVisible(true);
					break;
					
			case 3: this.currentDiceOne=new JLabel(this.diceThree);
					this.contentPane.add(this.currentDiceOne);
					this.currentDiceOne.setBounds(10, 20, 176, 208);
					this.currentDiceOne.setVisible(true);
					break;
					
			case 4: this.currentDiceOne=new JLabel(this.diceFour);
					this.contentPane.add(this.currentDiceOne);
					this.currentDiceOne.setBounds(10, 20, 176, 208);
					this.currentDiceOne.setVisible(true);
					break;
					
			case 5: this.currentDiceOne=new JLabel(this.diceFive);
					this.contentPane.add(this.currentDiceOne);
					this.currentDiceOne.setBounds(10, 20, 176, 208);
					this.currentDiceOne.setVisible(true);
					break;
					
			case 6: this.currentDiceOne=new JLabel(this.diceSix);
					this.contentPane.add(this.currentDiceOne);
					this.currentDiceOne.setBounds(10, 20, 176, 208);
					this.currentDiceOne.setVisible(true);
					break;
					
		}
		switch(y){
			case 1: this.currentDiceTwo=new JLabel(this.diceOne);
					this.contentPane.add(this.currentDiceTwo);
					this.currentDiceTwo.setBounds(200, 20, 176, 208);
					this.currentDiceTwo.setVisible(true);
					break;
					
			case 2: this.currentDiceTwo=new JLabel(this.diceTwo);
					this.contentPane.add(this.currentDiceTwo);
					this.currentDiceTwo.setBounds(200, 20, 176, 208);
					this.currentDiceTwo.setVisible(true);
					break;
					
			case 3: this.currentDiceTwo=new JLabel(this.diceThree);
					this.contentPane.add(this.currentDiceTwo);
					this.currentDiceTwo.setBounds(200, 20, 176, 208);
					this.currentDiceTwo.setVisible(true);
					break;
					
			case 4: this.currentDiceTwo=new JLabel(this.diceFour);
					this.contentPane.add(this.currentDiceTwo);
					this.currentDiceTwo.setBounds(200, 20, 176, 208);
					this.currentDiceTwo.setVisible(true);
					break;
					
			case 5: this.currentDiceTwo=new JLabel(this.diceFive);
					this.contentPane.add(this.currentDiceTwo);
					this.currentDiceTwo.setBounds(200, 20, 176, 208);
					this.currentDiceTwo.setVisible(true);
					break;
					
			case 6: this.currentDiceTwo=new JLabel(this.diceSix);
					this.contentPane.add(this.currentDiceTwo);
					this.currentDiceTwo.setBounds(200, 20, 176, 208);
					this.currentDiceTwo.setVisible(true);
					break;
					
		}
		
	}
}
